package com.zy.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zy.dao.CheckGroupDao;
import com.zy.entity.PageResult;
import com.zy.entity.QueryPageBean;
import com.zy.pojo.CheckGroup;
import com.zy.pojo.CheckItem;
import com.zy.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass=CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

	@Autowired
	private CheckGroupDao checkGroupDao;

	@Override
	public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
		//向管理组表中添加数据
		checkGroupDao.add(checkGroup);
		//得到新添加数据的id
		Integer checkGroupId = checkGroup.getId();
		//根据id往中间表中添加数据
		if(checkitemIds!=null && checkitemIds.length > 0){
			for (Integer checkitemId : checkitemIds) {
				checkGroupDao.setCheckGroupAndCheckItem(checkGroupId,checkitemId);
			}
		}
	}

	@Override
	public PageResult findPage(QueryPageBean queryBean) {
		Integer currentPage = queryBean.getCurrentPage();
		Integer pageSize = queryBean.getPageSize();
		String queryString = queryBean.getQueryString();
		PageHelper.startPage(currentPage, pageSize);
		Page<CheckGroup> page=checkGroupDao.findByCondition(queryString);
		List<CheckGroup> result = page.getResult();
		return new PageResult(page.getTotal(),result);
	}

	@Override
	public void delete(Integer id) {
		//把中间表的关联数据进行删除
		checkGroupDao.deleteCheckGroupAndCheckItem(id);
		checkGroupDao.deleteCheckGroupAndSetmeal(id);
		//删除该数据
		checkGroupDao.deleteCheckGroup(id);
	}

	@Override
	public List<Integer> findCheckitemAndCheckgroupId(Integer id) {
		return checkGroupDao.findCheckitemAndCheckgroupById(id);
	}

	@Override
	public void edit(Integer[] checkitemIds, CheckGroup checkGroup) {
		//修改基本信息
		checkGroupDao.edit(checkGroup);
		//删除中间表信息
		checkGroupDao.deleteCheckGroupAndCheckItem(checkGroup.getId());
		//向中间表添加信息
		if(checkitemIds!=null && checkitemIds.length > 0){
			for (Integer checkitemId : checkitemIds) {
				checkGroupDao.setCheckGroupAndCheckItem(checkGroup.getId(),checkitemId);
			}
		}
	}

	@Override
	public List<CheckGroup> finAll() {
		return checkGroupDao.finAll();
	}

	@Override
	public List<CheckGroup> findAllOrder(Integer id) {
		return checkGroupDao.findAllOrder(id);
	}

}
